package me.phelps.library;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOpreateUtils {

	/**
	 * @param fromFile 被复制的文件
	 * @param toFile   复制的目录文件
	 * @param rewrite  是否重新创建文件
	 *                 <p>
	 *                 <p>文件的复制操作方法
	 */
	public static void copyfile(File fromFile, File toFile, Boolean rewrite) throws CopyFailedException {

		if (!fromFile.exists( )) {
			throw new CopyFailedException( );
		}

		if (!fromFile.isFile( )) {
			throw new CopyFailedException( );
		}
		if (!fromFile.canRead( )) {
			throw new CopyFailedException( );
		}
		if (!toFile.getParentFile( ).exists( )) {
			toFile.getParentFile( ).mkdirs( );
		}
		if (toFile.exists( ) && rewrite) {
			toFile.delete( );
		}

		try {
			FileInputStream fosfrom = new FileInputStream(fromFile);
			FileOutputStream fosto = new FileOutputStream(toFile);

			byte[] bt = new byte[1024];
			int c;
			while ((c = fosfrom.read(bt)) > 0) {
				fosto.write(bt, 0, c);
			}
			//关闭输入、输出流
			fosfrom.close( );
			fosto.close( );


		} catch (FileNotFoundException e) {
			e.printStackTrace( );
			throw new CopyFailedException();
		} catch (IOException e) {
			e.printStackTrace( );
			throw new CopyFailedException();
		}

	}

	public static class CopyFailedException extends Exception {

	}
}  